<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字段：">
        <el-select v-model="options.key" placeholder="请选择查找字段" clearable>
          <el-option v-for="dict in options" :key="dict.value" :label="dict.label" :value="options.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="值：">
        <el-input v-model="queryParams.value" placeholder="请输入查找值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button size="mini" @click="handleAdd" type="primary" plain icon="el-icon-plus" >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="factoryList">
      <el-table-column label="能源类型" align="center" prop="ny" />
      <el-table-column label="能源消耗量" align="center" prop="nyxhl1" />
      <el-table-column label="地域范围" align="center" prop="dyfw1" />
      <el-table-column label="能源消耗结构（%）" align="center" prop="nyxhjg1" width="200" />
      <el-table-column label="能源消耗趋势" align="center" prop="nyxhqs1" />
      <el-table-column label="能源消耗排名" align="center" prop="nyxhpm1" />
      <el-table-column label="时间范围（日/周/月/年）" align="center" prop="sjfw1" min-width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pill:factory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pill:factory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改生产车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="能源类型" prop="ny">
          <el-input v-model="form.ny" placeholder="请输入能源类型" />
        </el-form-item>
        <el-form-item label="能源消耗量" prop="nyxhl1" label-width="100px">
          <el-input v-model="form.nyxhl1" placeholder="请输入能源消耗量" />
        </el-form-item>
        <el-form-item label="地域范围" prop="dyfw1">
          <el-input v-model="form.dyfw1" placeholder="请输入地域范围" />
        </el-form-item>
        <el-form-item label="能源消耗结构" prop="nyxhjg1" label-width="100px">
          <el-input v-model="form.nyxhjg1" placeholder="请输入能源消耗结构" />
        </el-form-item>
        <el-form-item label="能源消耗趋势" prop="nyxhqs1" label-width="100px">
          <el-input v-model="form.nyxhqs1" placeholder="请输入能源消耗趋势" />
        </el-form-item>
        <el-form-item label="能源消耗排名" prop="nyxhpm1" label-width="100px">
          <el-input v-model="form.nyxhpm1" placeholder="请输入能源消耗排名" />
        </el-form-item>
        <el-form-item label="时间范围" prop="sjfw1" label-width="100px">
          <el-input v-model="form.sjfw1" placeholder="请输入时间范围" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEnergyConsumption,
  postEnergyConsumption,
  delEnergyConsumption,
  updateEnergyConsumption,
} from "@/api/energyConsumption";
export default {
  name: "Factory",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产车辆信息表格数据
      factoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //字段
      options: [
        { value: 'ny', label: '能源类型' },
        { value: 'nyxhl1', label: '能源消耗量' },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        key: "ny",
        value: null,
        ny: "",
        nyxhl1: "",
      },
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产车辆信息列表 */
    getList() {
      this.loading = true;
      getEnergyConsumption(this.queryParams).then((response) => {
        this.factoryList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ny: null,
        nyxhl1: null,
        sjfw1: null,
        dyfw1: null,
        nyxhjg1: null,
        nyxhqs1: null,
        nyxhpm1: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
        pageSize: 10,
        key: "ny",
        value: null,
        ny: "",
        nyxhl1: "",
      };
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加能源信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      this.open = true;
      this.title = "修改能源信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEnergyConsumption(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            postEnergyConsumption(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Id = row.id;
      console.log(Id);
      this.$modal
        .confirm('是否确认删除能源信息"' + row.ny + '"的数据项？')
        .then(function () {
          return delEnergyConsumption(Id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
